/*******************************************************************************
 * SVN­Versioning-Information:
 * ­­­­­­­­­­­­­­­­­­­­­­­­
 * $LastChangedRevision: 130 $
 * $LastChangedBy: SD $
 * $LastChangedDate: 2009-06-23 17:42:49 +0200 (Tue, 23 Jun 2009) $
 * $HeadURL: svn://lvis10.fbm.fht-stuttgart.de/common/Common/src/de/hft_stuttgart/botwar/common/models/GameInfo.java $
 ******************************************************************************/
package de.hft_stuttgart.botwar.common.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * Objects of this class possess all the information
 * the client needs to start a new game.
 * @author c.a.duevel@gmail.com, sdegiorgi@gmail.com, pmv-mail@gmx.de
 */
public class GameInfo implements Serializable{
    private Map<String, List<RobotInfo>> playerToRobots =
            new HashMap<String, List<RobotInfo>>();
    /**
     * public accessable variable to check for the running state of a game
     */
    public boolean running = false;
    private String gameOwner;
    private final int gameID;
    private final String gameName;
    private final String mapName;
    private final int maxPlayers;
    private final int maxBots;
    private final int maxPlayerLevel;

    /**
     * New game info.
     * @param gameID of the current game.
     */
    public GameInfo(final int gameID) {
        this.gameID = gameID;
        this.gameOwner = "unknown";
        this.gameName = "";
        this.mapName = "";
        this.maxPlayers = 4;
        this.maxBots = 3;
        this.maxPlayerLevel = 99;
    }

    /**
     * Info of a game.
     * @param gameID of the current game
     * @param gameOwner of the current game
     * @param gameName of the current game
     * @param mapName of the current game
     * @param maxPlayers of the current game
     * @param maxBots of the current game
     * @param maxPlayerLevel of the current game
     */
    public GameInfo(final int gameID, final String gameOwner,
            final String gameName, final String mapName, final int maxPlayers,
            final int maxBots, final int maxPlayerLevel) {

        this.gameID = gameID;
        this.gameOwner = gameOwner;
        this.gameName = gameName;
        this.mapName = mapName;
        this.maxPlayers = maxPlayers;
        this.maxBots = maxBots;
        this.maxPlayerLevel = maxPlayerLevel;
    }

    /**
     * Adds a player to a game.
     * @param playerID of the added player.
     */
    public void addPlayer(final String playerID){
        playerToRobots.put(playerID, new ArrayList<RobotInfo>(maxBots));
    }

    /**
     * Adds a robot.
     * @param playerID of the player
     * @param robot info of the added robot
     */
    public void addRobot(final String playerID, final RobotInfo robot){
        playerToRobots.get(playerID).add(robot);
    }

    /**
     * Returns a list of robot info.
     * @param playerId of the robots.
     * @return playerToRobots.get(playerId) list of robot info.
     */
    public List<RobotInfo> getRobotInfos(final String playerId){
        return playerToRobots.get(playerId);
    }

    /**
     * Return all players.
     * @return list of all players.
     */
    public List<String> getPlayers(){
        List<String> list = new ArrayList<String>();
        for (String playerID : playerToRobots.keySet()) {
            list.add(playerID);
        }
        return list;
    }

    /**
     * Kick a player in a game.
     * @param playerID of the player which should removed.
     */
    public void removePlayer(final String playerID){
        playerToRobots.remove(playerID);
    }

    /**
     * Returns the number of robots a player has in the game.
     * @param playerID of all robots.
     * @return playerToRobots.get(playerID).size() int of count robots
     */
    public int countRobots(final String playerID){
        return playerToRobots.get(playerID).size();
    }

    /**
     * Returns the name of the game.
     * @return getName name of the game
     */
    public String getGameName() {
        return gameName;
    }

    /**
     * Returns the id of the owner of the game.
     * @return gameOwner
     */
    public String getGameOwner() {
        return gameOwner;
    }

    /**
     * Returns the name of the map.
     * @return mapName of the game
     */
    public String getMapName() {
        return mapName;
    }

    /**
     * Returns the number of max bots.
     * @return maxBots in a game
     */
    public int getMaxBots() {
        return maxBots;
    }

    /**
     * Returns the number of max players in a game.
     * @return maxPlayers of a game
     */
    public int getMaxPlayers() {
        return maxPlayers;
    }

    /**
     * Returns the max player level of a game.
     * @return maxPlayerLevel max player level
     */
    public int getMaxPlayerLevel() {
        return maxPlayerLevel;
    }

    /**
     * Returns the id of a game.
     * @return gameID id of the game
     */
    public int getGAME_ID() {
        return gameID;
    }

    /**
     * Overrides method equals.
     * @param obj to compare
     * @return boolean true or false
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final GameInfo other = (GameInfo) obj;
        if (this.playerToRobots != other.playerToRobots &&
                (this.playerToRobots == null ||
                !this.playerToRobots.equals(other.playerToRobots))) {
            return false;
        }
        if (this.running != other.running) {
            return false;
        }
        if (this.gameID != other.gameID) {
            return false;
        }
        return true;
    }

    /**
     * Overrides method hashCode.
     * @return hash code
     */
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 23 * hash + (this.playerToRobots != null ?
            this.playerToRobots.hashCode() : 0);
        hash = 23 * hash + (this.running ? 1 : 0);
        hash = 23 * hash + this.gameID;
        return hash;
    }



}
